/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.model.Transaction;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class TransactionListController extends BaseController {

    protected Transaction selectedTransaction;
    protected Listbox listboxTransaction;
    protected Search searchTransaction;
    protected List<Transaction> transactions;

    public Transaction getSelectedTransaction() {
        return this.selectedTransaction;
    }

    public void setSelectedTransaction(Transaction selectedTransaction) {
        this.selectedTransaction = selectedTransaction;
    }

    public List<Transaction> getTransactions() {
        return this.transactions;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchTransaction = new Search(Transaction.class);
        this.searchTransaction.setMaxResults(100);
        this.searchTransaction.addFetch("createdBy");
        this.searchTransaction.addFetch("updatedBy");
        this.transactions = this.transactionService.search(this.searchTransaction);

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemTransactionCreate(Event event) throws Exception {
        this.transactionCreate();
    }

    public void onClick$menuitemTransactionEdit(Event event) throws Exception {
        this.transactionEdit();
    }

    public void onClick$menuitemTransactionDelete(Event event) throws Exception {
        this.transactionDelete();
    }

    public void onClick$menuitemTransactionSearch(Event event) throws Exception {
        this.transactionSearch();
    }

    public void onClick$menuitemTransactionRefresh(Event event) throws Exception {
        this.transactionRefresh();
    }

    public void onClick$toolbarbuttonTransactionCreate(Event event) throws Exception {
        this.transactionCreate();
    }

    public void onClick$toolbarbuttonTransactionEdit(Event event) throws Exception {
        this.transactionEdit();
    }

    public void onClick$toolbarbuttonTransactionDelete(Event event) throws Exception {
        this.transactionDelete();
    }

    public void onClick$toolbarbuttonTransactionSearch(Event event) throws Exception {
        this.transactionSearch();
    }

    public void onClick$toolbarbuttonTransactionRefresh(Event event) throws Exception {
        this.transactionRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.transactionEdit();
    }
    
    private void transactionCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/transaction.zul");
    }

    private void transactionEdit() throws Exception {
        if (this.selectedTransaction != null) {
            this.execution.sendRedirect("~./zul/html/transaction.zul?Id=" + this.selectedTransaction.getId().toString());
        }
    }

    private void transactionDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxTransaction.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    Transaction transaction = (Transaction) selectedItem.getValue();
                    this.transactionService.remove(transaction);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.transactions = this.transactionService.search(this.searchTransaction);
        this.binder.loadAll();
    }

    private void transactionSearch() throws Exception {
        this.execution.setAttribute("searchTransaction", this.searchTransaction);

        Window window = (Window) this.execution.createComponents("~./zul/html/transactionSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchTransaction = (Search) this.execution.getAttribute("searchTransaction");
        }

        this.transactions = this.transactionService.search(this.searchTransaction);
        this.binder.loadAll();
    }

    private void transactionRefresh() {
        this.transactions = this.transactionService.search(this.searchTransaction);
        this.binder.loadAll();
    }
}
